#!/bin/bash
DEBUG=
#
# bckwin.sh
#
# author:   MaRZ <marzvix@outlook.com>
# creation: Sat Jul 28 10:36:25 BRT 2011
#
# Script to drive an windows application to run it backup
#   and save results in this LOCAL machine
#
# note: extension .sh have to be removed before install
#       in proper cron dir
#
#

#display source on debugging
test -z ${DEBUG} || set -x

#define file creation mask
umask 022

# divide date and generate date stamp in iso format
THISYEAR="`date +'%Y'`"
THISMON="`date +'%m'`"
THISDAY="`date +'%d'`"
ISO_TODAY="${THISYEAR}${THISMON}${THISDAY}"

# LOCAL is where this script is running
LOCAL_RCTOOLPATH="/usr/bin" 
LOCAL_RCTOOLEXE="winexe" 

# HOST is remote machine
HOST="//10.0.0.13"
HOST_USER="-U user"
HOST_PASS="--password password"
HOST_TEMPPATH="C:\Users\Public\Backups"

# Databases to be saved
BD_MAX=5
BD=("DATABASE1.FDB" "DATABASE2.GDB" "DATABASE3.GDB" "DATABASE4.GDB" "DATABASE5.GDB"))
BD_USER=("USERDB1" "USERDB2" "USERDB3" "USERDB4" "USERDB5")
BD_PASS=("pass1" "pass2" "pass2"  "pass3"  "pass4")
BD_PATH= ##"C:\Database dir\" # didn't used - aliases are used
# ARCHIVES
ARCHSET_SUFFIX=(".fbk" ".fbk" ".gbk" ".gbk" ".gbk")
ARCHSET_PREFIX="${ISO_TODAY}_"
ARCH_DESTDIR="/srv/BACKUPS/${THISYEAR}/${THISMON}"

## Tool chain

# RC is our remote control tool (winexe in this case)
RCTOOL="${LOCAL_RCTOOLPATH}/${LOCAL_RCTOOLEXE}"

# Tool that really executes backup in HOST (gbak from firebird in this case)
BCKTOOL_PATH="C:\Program Files\Firebird\Firebird_2_5\bin"
BCKTOOL_EXE="gbak.exe"
BCKTOOL=${BCKTOOL_PATH}\\${BCKTOOL_EXE}
BCKTOOL_CMD="-b"

# Tool for compression (7-zip at HOST) because is better compress before transfer
ZIPTOOL_PATH="C:\Program Files\7-Zip"
ZIPTOOL_EXE="7z.exe"
ZIPTOOL="cmd.exe /c \"${ZIPTOOL_PATH}\\${ZIPTOOL_EXE}\""
ZIPTOOL_CMD="a"
ZIPTOOL_GENOPTS="-tgzip"
ZIP_ARCHIVE_SUFFIX=".gz"
ZIP_ARCHIVEPATH="C:\Windows\Temp"
ZIP_ARCHIVE=

# Tool for transfer saveset (compressed) from HOST to LOCAL (LOCAL resident, smbclient)
SMBTOOL_PATH="/usr/bin"
SMBTOOL_EXE="smbclient"
SMBTOOL="${SMBTOOL_PATH}/${SMBTOOL_EXE}"

SMBTOOL_USER="user%password"
SMBTOOL_SHARE="${HOST}/c$"
SMBTOOL_HOSTWORKDIR="/Windows/Temp"
SMBTOOL_TMPDIR="/tmp"

if [ ! -d ${ARCH_DESTDIR} ]; then
    echo "Generating ${ARCH_DESTDIR}..."
    mkdir -p ${ARCH_DESTDIR}
    if ![ $? == 0 ]; then 
	echo "bckwin: cannot create ${ARCH_DESTDIR} " 
	exit -1
    fi
    chown -R user:group ${ARCH_DESTDIR}
    echo "bckwin: ${ARCH_DESTDIR} created"
fi

# Once in a month, clean repository (temp) at HOST
if [ ${THISDAY} -eq 1 ]; then
    echo "Cleaning repository (temp) of host"
    ${RCTOOL} ${HOST_USER} ${HOST_PASS} ${HOST} "cmd /c del ${HOST_TEMPPATH}\\*.?bk.gz"
fi

noecho() {
    test -z ${DEBUG} || echo $1  
}

###
### Showtime...
###

for i in `seq 0 $((${BD_MAX} - 1))`; do
#for i in `seq 0 0`; do
    ARCHSET_BD="${BD[i],,}"
    ARCHSET_BASENAME="${ARCHSET_BD%%.*}"
    ARCHSET_NAME="${ARCHSET_PREFIX}${ARCHSET_BASENAME}${ARCHSET_SUFFIX}"
    ARCHSET_TEMPNAME="${ARCHSET_PREFIX}${ARCHSET_BASENAME}_$$${ARCHSET_SUFFIX}"

    HOST_BD="${BD[i]}"
    HOST_ARCHTEMPSET="${HOST_TEMPPATH}\\${ARCHSET_TEMPNAME}"
    BCKTOOL_GENOPTS="-USER ${BD_USER[i]} -PAS ${BD_PASS[i]}"

    noecho "Backup of ${ARCHSET_BD}"
    ${RCTOOL} ${HOST_USER} ${HOST_PASS} ${HOST} "${BCKTOOL} ${BCKTOOL_CMD} ${HOST_BD} ${HOST_ARCHTEMPSET} ${BCKTOOL_GENOPTS}"

    ZIP_ARCHIVE="${ZIP_ARCHIVEPATH}\\${ARCHSET_TEMPNAME}${ZIP_ARCHIVE_SUFFIX}"
    noecho "Compressing ${ZIP_ARCHIVE}"
    ${RCTOOL} ${HOST_USER} ${HOST_PASS} ${HOST} "${ZIPTOOL} ${ZIPTOOL_CMD} ${ZIPTOOL_GENOPTS} ${ZIP_ARCHIVE} ${HOST_ARCHTEMPSET} > nul"

    noecho "Removing decompressed archive"
    ${RCTOOL} ${HOST_USER} ${HOST_PASS} ${HOST} "cmd /c del ${HOST_ARCHTEMPSET}"
    
    noecho "Transfering to ${SMBTOOL_TMPDIR}"
    SMBTOOL_CMD="-c get ${ARCHSET_TEMPNAME}${ZIP_ARCHIVE_SUFFIX} ${SMBTOOL_TMPDIR}/${ARCHSET_TEMPNAME}${ZIP_ARCHIVE_SUFFIX}"
    ${SMBTOOL} -U ${SMBTOOL_USER} -D ${SMBTOOL_HOSTWORKDIR} ${SMBTOOL_SHARE} "${SMBTOOL_CMD}"

    noecho "Removing compressed archive"
    ${RCTOOL} ${HOST_USER} ${HOST_PASS} ${HOST} "cmd /c del ${ZIP_ARCHIVE}"

    noecho "Moving to ${ARCH_DESTDIR}"
    mv ${SMBTOOL_TMPDIR}/${ARCHSET_TEMPNAME}${ZIP_ARCHIVE_SUFFIX} ${ARCH_DESTDIR}
done
